<?php
include_once ("../QuizUtils.php");
include_once ('../counter.php');
include_once ("../quizzes/Business/user_service.php");
include_once ("../quizzes/Business/survey_answer_service.php");
include_once ("../quizzes/Business/survey_question_service.php");
include_once ("../quizzes/Business/survey_answer_user_service.php");
include_once ("../quizzes/Business/survey_result_service.php");
include_once ("../quizzes/Business/survey_service.php");
include_once ("../quizzes/Business/Data/Constants.php");

if(isset($_GET["id"])){
	$content='<head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"></head>';
	$survey_id=$_GET["id"];
	$survey =survey_service::get_survey($survey_id);
	$user_id =1;
	if($survey){
			
		
		$answer_list  =  survey_answer_service::get_survey_answer_list_by_user_survey($user_id,$survey_id);
		if($answer_list!=null){
			//display result
			$mark = 0;
			foreach ($answer_list as $answer){
				$mark =  $mark + $answer->survey_answer_mark;
			}
			include_once 'success_result.php';
		}else{
			//display survey
			$content =$content.'<form method="post">';
			$content =$content.'<h1>'.stripslashes($survey->survey_name).'</h1>	';

			$sq = survey_question_service::get_survey_question_list_by_survey_id($survey_id);
			$nq = count($sq);
			$temp_sq_id='';
			for($i=1 ; $i<=$nq ; $i++) {
				$q = $sq[$i-1];

				$content =$content.'<h2>'.$i.'.'.stripslashes($q->survey_question_content).'</h2>';

				if($i==1){
					$temp_sq_id=$temp_sq_id.$q->survey_question_id;
				}else{
					$temp_sq_id=$temp_sq_id.','.$q->survey_question_id;
				}

				$sa = survey_answer_service::get_survey_answer_list_by_survey_question_id($q->survey_question_id);
				for($j=1 ; $j<=count($sa) ; $j++) {
					$content =$content.'<input type="radio" name="'.$q->survey_question_id.'" value="'.$sa[$j-1]->survey_answer_id.';'.$sa[$j-1]->survey_answer_mark.'" >'.stripslashes($sa[$j-1]->survey_answer_content).'<br/>';


				}
			}
			$content =$content.'<input type="hidden" name="survey_question" value='.$temp_sq_id.'>';
			$content =$content.'<input type="hidden" name="survey_id" value='.$survey_id.'>';
			$content =$content.'<input type="hidden" name="user_id" value='.$user_id.'>';
			$content =$content.'<input type="hidden" name="mode" value=result>';
			$content =$content.'<br>';
			$content =$content.'<input type="submit" value="Hoàn tất">';
			$content =$content.'</form>';
			$counter  =  new counter($survey_id);
			$content = $content.$counter->build_gui();
		}

	}else{
		$content =$content."id not found";
	}
}
else {
	$content =$content."id not found";
}
?>
